<?php
    Class Solution {
        /**
         * 566. 重塑矩阵
         * @param Integer[][] $mat
         * @param Integer $r
         * @param Integer $c
         * @return Integer[][]
         */
        function matrixReshape566($mat, $r, $c) {
            $new = [];
            $count = 0;
            $m = 0;
            $n = 0;
            foreach($mat as $mat1){
                foreach($mat1 as $mat2){
                    if($n < $c){
                        $new[$m][] = $mat2;
                        $n++;
                    }else{
                        $m++;
                        $new[$m][] = $mat2;
                        $n = 1;
                    }
                    $count++;
                }
            }

            return $count == $r * $c? $new : $mat;
        }

        /**
         * 575. 分糖果
         * @param Integer[] $candyType
         * @return Integer
         */
        function distributeCandies575($candyType) {
            $map = [];
            foreach($candyType as $type){
                if(!isset($map[$type])){
                    $map[$type] = [1];
                }
            }

            return min(count($map), count($candyType)/2);
        }

        /**
         * 598. 区间加法 II
         * @param Integer $m
         * @param Integer $n
         * @param Integer[][] $ops
         * @return Integer
         */
        function maxCount598($m, $n, $ops) {
            if(!$ops){
                return $m * $n;
            }
            $map1 = [];
            $map2 = [];
            foreach($ops as $op){
                $map1[] = $op[0];
                $map2[] = $op[1];
            }

            return min($map1) * min($map2);
        }

        /**
         * 599. 两个列表的最小索引总和
         * @param String[] $list1
         * @param String[] $list2
         * @return String[]
         */
        function findRestaurant599($list1, $list2) {
            $map   = [];
            $match = [];
            foreach($list1 as $key => $item){
                if(!isset($map[$item])){
                    $map[$item] = $key;
                }
            }
            foreach($list2 as $key => $item){
                if(isset($map[$item])){
                    if(!isset($match[$item])){
                        $match[$item] = $map[$item] + $key;
                    }
                }
            }
            $map = [];
            foreach($match as $key => $item){
                if(!isset($small)){
                    $small = $item;
                    $map[] = $key;
                    continue;
                }
                if($item < $small){
                    $small = $item;
                    $map = [$key];
                }elseif($item == $small){
                    $map[] = $key;
                }
            }

            return $map;
        }

        /**
         * 605. 种花问题
         * @param Integer[] $flowerbed
         * @param Integer $n
         * @return Boolean
         */
        function canPlaceFlowers605($flowerbed, $n) {
            $x = 0;
            while($n > 0 && $x <= (count($flowerbed) - 1)){
                if($flowerbed[$x-1] != 1){
                    if($flowerbed[$x] != 1){
                        if($flowerbed[$x+1] != 1){
                            $flowerbed[$x] = 1;
                            $n--;
                            $x += 2;
                        }else{
                            $x += 3;
                        }
                    }else{
                        $x += 2;
                    }
                }else{
                    $x += 1;
                }
            }

            return $n <= 0;
        }

        /**
         * 628. 三个数的最大乘积
         * @param Integer[] $nums
         * @return Integer
         */
        function maximumProduct628($nums) {
            $count = count($nums);
            sort($nums);
            return max(array(
                $nums[0]*$nums[1]*$nums[$count-1],
                $nums[$count-1]*$nums[$count-2]*$nums[$count-3]
                ));
        }

        /**
         * 643. 子数组最大平均数 I
         * @param Integer[] $nums
         * @param Integer $k
         * @return Float
         */
        function findMaxAverage643($nums, $k) {
            $s = 0;
            for($i = 0;$i < count($nums);$i++){
                $s += $nums[$i];
                if($i < $k-1){
                    continue;
                }
                if(!isset($max) || $max < $s){
                    $max = $s;
                }
                $s -= $nums[$i-$k+1];
            }

            return $max/$k;
        }

        /**
         * 645. 错误的集合
         * @param Integer[] $nums
         * @return Integer[]
         */
        function findErrorNums645($nums) {
            $map = [];
            foreach($nums as $num){
                if(!isset($map[$num])){
                    $map[$num] = $num;
                }else{
                    return [$num,(1+count($nums))*count($nums)/2-array_sum($nums)+$num];
                }
            }
        }

        /**
         * 657. 机器人能否返回原点
         * @param String $moves
         * @return Boolean
         */
        function judgeCircle657($moves) {
            $U = 0;
            $L = 0;
            for($i = 0; $i < strlen($moves); $i++){
                switch($moves[$i]){
                    case 'U':
                        $U++;
                        break;
                    case 'L':
                        $L++;
                        break;
                    case 'D';
                        $U--;
                        break;
                    case 'R':
                        $L--;
                        break;
                }
            }
            return !$U && !$L;
        }

        /**
         * 661. 图片平滑器
         * @param Integer[][] $img
         * @return Integer[][]
         */
        function imageSmoother661($img) {
            $map = [];
            foreach($img as $k1 => $v1){
                foreach($v1 as $k2 => $v2){
                    $count = 0;
                    $sum = 0;
                    for($i = $k1-1; $i < $k1+2; $i++){
                        for($j = $k2-1; $j < $k2+2; $j++){
                            if(isset($img[$i][$j])){
                                $sum += $img[$i][$j];
                                $count ++;
                            }
                        }
                    }
                    $map[$k1][$k2] = intval($sum/$count);
                }
            }

            return $map;
        }

    }

$img =[[1]];
    $k = 4;
    $solution = new Solution();
    print_r($solution->imageSmoother661($img));

?>